package ru.sushka.lunch.dao.psql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import ru.sushka.lunch.enitiy.MenuItem;

public class PsqlMenuItemDao extends PsqlDao<MenuItem>{

    private final String tableName = "menuitems";
    
    @Override
    protected String getTableName() {
        return tableName;
    }

    @Override
    protected String getSelectSqlQuery(int id) {
        return "select * from " + tableName + " where id = " + id;
    }

    @Override
    protected String getDeleteSqlQuery(int id) {
        return "delete from " + tableName + " where id = " + id;
    }

    @Override
    protected String getInsertSqlQuery(MenuItem persistence) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(persistence.getDate().getTime());
        return "insert into " + tableName + "(course_id, item_date) values(" + 
                                persistence.getCourseId() + ",'" + date +"')";
    }

    @Override
    protected String getUpdateSqlQuery(MenuItem persistence) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(persistence.getDate().getTime());
        return "update customers set id=" + persistence.getId() + 
                                        "course_id = "+persistence.getCourseId()+","+
                                        "item_date ='"+date+"' "+
                                        "where id=" + persistence.getId();
    }

    @Override
    protected MenuItem parseResultSet(ResultSet rs) throws SQLException{
        int id = rs.getInt(1);
        int courseId = rs.getInt(2);
        Calendar menuDate = Calendar.getInstance();
        menuDate.setTime(rs.getDate(3));
        MenuItem menuitem = new MenuItem(courseId, menuDate);
        menuitem.setId(id);
        return menuitem;

    }

}
